package com.lcxsoft.paymentservice.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.lcxsoft.commonutils.JwtUtils;
import com.lcxsoft.paymentservice.query.PaymentQuery;
import com.lcxsoft.paymentservice.service.PaymentService;
import com.lcxsoft.paymentservice.url.PaymentServiceUrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping(PaymentServiceUrl.packageUrl)
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    /**
     * 支付接口
     * @param query
     * @param request
     * @param httpResponse
     * @throws Exception
     */
    @RequestMapping(value = PaymentServiceUrl.PaymentUrl.pay, method = RequestMethod.GET)
    public void pay(PaymentQuery query, HttpServletRequest request, HttpServletResponse httpResponse) throws Exception {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        query.setToken(memberIdByJwtToken);
        paymentService.pay(query, request, httpResponse);

    }

    /**
     * 异步通知接受
     * @param request
     * @throws Exception
     */
    @RequestMapping(value = PaymentServiceUrl.PaymentUrl.payNotify, method = RequestMethod.POST)
    public void payNotify(HttpServletRequest request) throws Exception {
        paymentService.payNotify(request);

    }






}
